﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace GameJam
{
    public class GameUI : MonoBehaviour
    {
        [SerializeField]
        List<Image> ready_point_list;
        [SerializeField]
        Text counter_text;
        public Camera uiCamera;
        public Transform tips_obj;
        // Use this for initialization
        void Start()
        {

        }

        public void ShowTips()
        {
            tips_obj.transform.localPosition = Vector3.zero;
        }

        public void HideTips()
        {
            tips_obj.transform.localPosition = new Vector3(-10000,-10000,0);
        }

        public void ResetAllPlayerToUnReady()
        {
            foreach(Image imgae in ready_point_list)
            {
                imgae.color = new Color(1,0,0);
            }
            StopCoroutine("DelayIEnumerator");
            counter_text.enabled = false;

        }

        public void SetIsReady(int player_index,bool isReady,Vector3 pos)
        {
            ready_point_list[player_index].enabled = true;
            ready_point_list[player_index].rectTransform.localPosition = pos;
            if (isReady)
            {
                ready_point_list[player_index].color = new Color(0, 1, 0);
            }
            else
            {
                ready_point_list[player_index].color = new Color(1, 0, 0);
            }
        }

        public void StartGameCounter()
        {
            StartCoroutine("DelayIEnumerator");
        }

        IEnumerator DelayIEnumerator()
        {
            yield return new WaitForSeconds(0.5f);
            HideAllReadyPoint();
            counter_text.enabled = true;
            int timer = 3;
            while(timer >= 0)
            {
                if (timer != 0)
                {
                    counter_text.text = timer.ToString();
                }
                else
                {
                    counter_text.text = "Go";
                }
                timer--;
                yield return new WaitForSeconds(1);
            }
            counter_text.enabled = false;
        }

        private void HideAllReadyPoint()
        {
            foreach (Image imgae in ready_point_list)
            {
                imgae.enabled = false;
            }
        }
    }
}